import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import javax.swing.*;

public class CircleSegment extends JPanel  {
	
	private static final long serialVersionUID = 1L;
	protected IDataSampleable iSample;
	protected double zoom = 1.0; 
	protected double zoomPercentage;
	protected int rotate = 0;
	    
    public CircleSegment(double percentage)
    {
    	zoomPercentage = percentage / 100;
    	
    	ColorMap.GenerateColorMap();
    }
    	
	public void init()
	{
		setBackground(Constant.bg);
	}
	
	public void setSample(IDataSampleable sample)
	{
		iSample = sample;
	}
	public void clear()
	{
		iSample = null;
		zoom = 1.0;
		rotate = 0;
	}
	
	public void zoomIn()
	{
		zoom += zoomPercentage; 		
	}
	
	public void zoomOut()
	{
		zoom -= zoomPercentage; 
        
        if(zoom < zoomPercentage) 
        { 
            if(zoomPercentage > 1.0) 
            { 
                zoom = 1.0; 
            } 
            else 
            { 
                zoomIn(); 
            } 
        } 
	}
	
	public void rotateLeft()
	{
		rotate -= 1;
	}
	
	public void rotateRight()
	{
		rotate += 1;
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		int rowSize = 0;
		int columnSize = 0;
				
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		init();
		
		if(iSample != null)
		{		
			rowSize = iSample.getRowSize();
			columnSize = iSample.getColumnSize();
			
			AffineTransform at = new AffineTransform();
			at.translate(Constant.CenterX, Constant.CenterY);
			at.scale(zoom, zoom);
			
			for(int rowIndex = 0; rowIndex < rowSize; rowIndex++)
			{
				Point points[] = PolygonPointCalculation.GetVertex(rowIndex, columnSize);			
				
				for(int columnIndex = 0; columnIndex < columnSize; columnIndex++)
				{
					int index = (columnIndex + rotate) % columnSize;
					if(index < 0) 
						index += columnSize;
					Color color = iSample.getColor(rowIndex, columnIndex);
					g2.setColor(color);
					int next = ((columnIndex + rotate +1) % columnSize );
					if(next < 0)
						next += columnSize;
					g2.draw(at.createTransformedShape(new Line2D.Double(points[index].x, points[index].y, points[next].x, points[next].y)));		
				}
				if(rowIndex == rowSize - 1)
				{
					g2.setColor(Color.black);
					for(int i = 0; i < points.length; i++)
					{
						Font font = new Font("Arial Narrow", Font.ITALIC, 20);
					    FontRenderContext fontRenderContext = g2.getFontRenderContext();
					    
					    int index = (i + rotate) % points.length;
					    int next = (i + rotate + 1) % points.length;
					    
					    if(index < 0) 
							index += points.length;
					    if(next < 0)
					    	next += points.length;
					    
					    int middleX = (points[index].x + points[next].x)/2;
					    int middleY = (points[index].y + points[next].y)/2;
					    
					    Point2D.Double pDest = new Point2D.Double();
					    at.transform(new Point2D.Double(middleX, middleY), pDest);
					    GlyphVector glyphVector = font.createGlyphVector(fontRenderContext,
					    								iSample.getHeaders().get(i));
					    AffineTransform aff = AffineTransform.getTranslateInstance(pDest.x, pDest.y);
						//aff.translate(Constant.CenterX, Constant.CenterY);						
						aff.rotate(middleX, middleY);
						Shape shape1 = glyphVector.getOutline();
					    Shape shape2 = aff.createTransformedShape(shape1);
					    g2.fill(shape2);
						
					}
				}
			}
		}
		ColorMap.drawColorMap(g2, 20, 60);
		

	}
}
